package com.huadi.ssm.Controller;

import com.huadi.ssm.Service.EvidenceService;
import com.huadi.ssm.Service.UserService;
import com.huadi.ssm.po.Evidence;
import com.huadi.ssm.po.Uprecord;
import com.huadi.ssm.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.w3c.dom.ls.LSException;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {
    //1：创建UserService调用Service层的方法
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    @Autowired
    @Qualifier("EvidenceServiceImpl")
    private EvidenceService evidenceService;

    //2:跳转到用户登录的界面
    @RequestMapping("/login")
    public String tologin(){ return "/login";}
    //3：用户进行登录操作，如果登录成功则返回相应的操作界面(此处用展示所有用户界面代替），失败则留在登录界面
    @RequestMapping("/tologin")
    public String login(String userId, String userPassword, Model model, HttpServletRequest request,HttpSession session) {
        User user1=(User)session.getAttribute("user");

        if(user1!=null)
        {
            switch (user1.getUserdepartment())
            {
                case "中心部门" :return "CentralDepartment";//进入中心部门的页面
                case "消防部门" : return "fireman";

                //进入消防部门的页面
                case "公安部门" :return "Policeman";//进入公安部门的页面
                case "技术部门" :return "Technician";//进入技术部门的页面
                default:return "login";
            }
        }
        User user = userService.login(userId, userPassword);
       if(user==null){
           return "login";
       }else{
           request.getSession().setAttribute("user",user);
           switch (user.getUserdepartment()){
           case "中心部门" :return "CentralDepartment";//进入中心部门的页面
           case "消防部门" : return "fireman";

           //进入消防部门的页面
           case "公安部门" :return "Policeman";//进入公安部门的页面
           case "技术部门" :return "Technician";//进入技术部门的页面
           default:return "login";
       }
    }
    }
    //4：查看所有的用户表单
    @RequestMapping("/alluser")
    public String getalluser(Model model){
        List<User> users=userService.findAllUser();
//        List<User> users1=new ArrayList<>();
//        for(User user:users){
//            if(user.getUserdepartment().equals("技术部门")){
//                users1.add(user);
//            }
//        }
//        List<Object> objects=new ArrayList<>();
        model.addAttribute("user",users);
        return "/alluser"; }

    //5:进入到添加用户的界面
    @RequestMapping("/toadduser")
    public String toadduser(){return "addUser";}

    //6：注册新的用户
    @RequestMapping("/addUser")
    public String addUser(User user){
        userService.addUser(user);
        return "redirect:/user/alluser";//重定向到所有用户的展示页面，查看用户注册是否成功
    }
    //8：删除用户

        @RequestMapping("/deleteuser")
     public String deleteuser(String userId)      {
        userService.deleteUserByUserId(userId);
        return "redirect:/user/alluser";
    }
    //9:进入更新用户的界面
    @RequestMapping("/toupdateuser")
    public String toupdateuser(HttpSession session){
        return "updateUser";
    }
    //10:更新用户
    @RequestMapping("/updateuser")
    public String updateuser(User user){
        userService.updateUser(user);
        return "redirect:/user/alluser";
    }



   //11:进入到修改密码页面
    @RequestMapping("/tochangePassword")
    public String tochangePassword(){return "changePassword";}

    //12:修改密码
    @RequestMapping("/changgePassword")
    public String changgePassword(String userId,String OldPassword,String NewPassword){
        User user=userService.login(userId,OldPassword);
        if(user==null){
            return "changePassword";
        }
        else{
            userService.changePassword(userId,NewPassword);
            return "redirect:/user/login";
        }
    }

  //12:进入到展示用户信息的页面
    @RequestMapping("/userInformation")
    public String userInformation(Model model,HttpSession session){
       User user=(User) session.getAttribute("user");
      model.addAttribute("user",user);
      return "userInformation";
    }

    //13：进入到修改个人用户信息的页面

    @RequestMapping("/tochanggeUserInformation")
    public String tochanggeUserInformation(){return "changgeUserInformation";}



@RequestMapping("/changgeUserInformation")
public String changeUserInformation(User user,HttpSession session){
        User user1=(User)session.getAttribute("user");
        user.setUserId(user1.getUserId());
        user.setUserdepartment(user1.getUserdepartment());
        user.setUsersex(user1.getUsersex());
        user.setUserPassword(user1.getUserPassword());
        userService.updateUser(user);
        return "/userInformation";

    }
}

